#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "OLED_Data.h"
#include "MyRTC.h"
#include "Convert.h"
#include "Key.h"
#include "Timer.h"
#include "ds18b20.h"
#include "led.h"

// 编码格式 GB2312

#define YEAR 	1
#define MONTH 	2
#define DAY 	3
#define HOUR 	4
#define MIN		5
#define SENCEND	6


void OLED_Proc(void);
void KEY_Proc(void);

void Key_scan (void);
	
char OLED_Show_text[30];	//定义LCD显示 内容
unsigned char KeyNumber;
uint32_t Timer_1ms;
uint32_t OLED_Tick;

unsigned char view_menu;
unsigned char view_temperature;

unsigned char oled_flag_off;
unsigned char set_temp_flag;
unsigned char set_time_ok;

unsigned char flag_temper_read;
float Temperature;

uint32_t uwtick_my;
uint32_t my_KEY_tick;

char *ChDay[] = {"*", "初一", "初二", "初三", "初四", "初五",
                       "初六", "初七", "初八", "初九", "初十",
                       "十一", "十二", "十三", "十四", "十五",
                       "十六", "十七", "十八", "十九", "二十",
                       "廿一", "廿二", "廿三", "廿四", "廿五",
                       "廿六", "廿七", "廿八", "廿九", "三十"};
char *ChMonth[] = {"*", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "冬月", "腊月"};

int main(void)
{
	/*模块初始化*/
	LED_Init();
	OLED_Init();		//OLED初始化
	OLED_Clear();
	MyRTC_Init();		//RTC初始化
	Key_Init();
	Timer_Init();
//	MyRTC_SetTime();
	Convert_to_Lunar();
	DS18B20_Init();
	
	/*显示静态字符串*/
	OLED_ShowImage(0,0, 16, 16, Battery);	
//	OLED_ShowString(0, 0, "   Calendar", OLED_8X16);
//	OLED_ShowString(0, 15, "Solar:XXXX-XX-XX", OLED_8X16);
//	OLED_ShowString(0, 30, "Lunor:", OLED_8X16);
//	OLED_Printf(0, 30, OLED_8X16, "Lunor:");
//	OLED_ShowString(0, 45, "Time :XX:XX:XX", OLED_8X16);	
	OLED_Update();
	/*开启时钟*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);		//开启PWR的时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);		//开启BKP的时钟
	
	/*备份寄存器访问使能*/
	PWR_BackupAccessCmd(ENABLE);							//使用PWR开启对备份寄存器的访问
	
	DS18B20_ConvertT();//先转换一次，延时1s，覆盖默认值
	Delay_ms(1000);
	
	while (1)
	{		
		//Key_scan();
		KeyNumber = Key_GetNum();

		KEY_Proc();	
		MyRTC_ReadTime();							//RTC读取时间，最新的时间存储到MyRTC_Time数组中		
		OLED_Proc();
		
		if (MyRTC_Time[3] == 0 && MyRTC_Time[4] == 0 && MyRTC_Time[5] == 0)
		{
			hjz lunar = toLunar(osolar); // 阳历转阴历
			Convert_to_Lunar();
		}
		
		// 温度显示
		if (flag_temper_read == 1)
		{
			DS18B20_ConvertT();	 //温度转换
			Temperature = DS18B20_ReadT(); //温度读取
			
			flag_temper_read = 0;
		}
		
	}
}


void OLED_Proc(void)
{
	//函数减速
	if (Timer_1ms - OLED_Tick < 300)
		return;
	OLED_Tick = Timer_1ms;
	
	oled_flag_off = !oled_flag_off;
	
	if (view_temperature == 0)	//第一个大界面
	{
		if (view_menu == 0)	//最初的界面
		{
			OLED_ShowImage(0,0, 16, 16, Battery);	
			OLED_ShowString(30, 0, "Calendar", OLED_8X16);
			sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", MyRTC_Time[0], MyRTC_Time[1], MyRTC_Time[2]);	//年月日 - 阳历
			OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);
			
			OLED_Printf(0, 30, OLED_8X16, "Lunor:");		//阴历
			OLED_ShowChinese(8*6, 30, ChMonth[My_lunar[1]]);
			OLED_ShowChinese(8*10, 30, ChDay[(char)(My_lunar[2])]);
				
			sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", MyRTC_Time[3], MyRTC_Time[4], MyRTC_Time[5]);	//当前时间
			OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);
			set_temp_flag = 1;	//
		}
		else
		{
			if (oled_flag_off == 0)
			{
				sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
				OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);			
				sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", Set_time_temp[3], Set_time_temp[4], Set_time_temp[5]);	//显示当前时间
				OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);
			}
			else if (oled_flag_off == 1)
			{			
				if (view_menu == YEAR)
				{			
					if (set_temp_flag == 1)	//进来赋值一次
					{
						Set_time_temp[0] = MyRTC_Time[0];
						Set_time_temp[1] = MyRTC_Time[1];
						Set_time_temp[2] = MyRTC_Time[2];
						Set_time_temp[3] = MyRTC_Time[3];
						Set_time_temp[4] = MyRTC_Time[4];
						Set_time_temp[5] = MyRTC_Time[5];
						
						set_temp_flag = 0;
					}
					
					sprintf(OLED_Show_text, "Solar:    -%02d-%02d", Set_time_temp[1], Set_time_temp[2]);	// 月日 - 阳历
					OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);				
							
					sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", Set_time_temp[3], Set_time_temp[4], Set_time_temp[5]);	//
					OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);					
				}
				else if (view_menu == MONTH) 
				{	
					sprintf(OLED_Show_text, "Solar:%04d-  -%02d", Set_time_temp[0], Set_time_temp[2]);	//年 日 - 阳历
					OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);
					sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", Set_time_temp[3], Set_time_temp[4], Set_time_temp[5]);	//
					OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);	
				}
				else if (view_menu == DAY ) 
				{	
					sprintf(OLED_Show_text, "Solar:%04d-%02d-  ", Set_time_temp[0], Set_time_temp[1]);	//年月 - 阳历
					OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);
				}
				else if (view_menu == HOUR) 
				{	
					sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
					OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);
					sprintf(OLED_Show_text, "Time:   :%02d:%02d", Set_time_temp[4], Set_time_temp[5]);	//年月日 - 阳历
					OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);
				}
				else if (view_menu == MIN) 
				{
					sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
					OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);
					sprintf(OLED_Show_text, "Time: %02d:  :%02d", Set_time_temp[3], Set_time_temp[5]);	//年月日 - 阳历
					OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);
				}
				else if (view_menu == SENCEND) 
				{	
					sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
					OLED_ShowString(0, 15, OLED_Show_text, OLED_8X16);
					sprintf(OLED_Show_text, "Time: %02d:%02d:  ", Set_time_temp[3], Set_time_temp[4]);
					OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);
				}
			}			
		}
		

		OLED_Update();
	}	
	else if (view_temperature == 1)
	{
		OLED_ShowString(0, 10, "  Temperature  ", OLED_8X16);
		
		sprintf(OLED_Show_text, "   %.2f", Temperature);
		OLED_ShowString(0, 30, OLED_Show_text, OLED_8X16);
		OLED_ShowChinese(8*9, 30, "℃");
		
		if (MyRTC_Time[6] == 0)
			sprintf(OLED_Show_text, "   Sunday      ");
		else if (MyRTC_Time[6] == 1)
			sprintf(OLED_Show_text, "   Monday      ");
		else if (MyRTC_Time[6] == 2)
			sprintf(OLED_Show_text, "   Tuesday     ");
		else if (MyRTC_Time[6] == 3)
			sprintf(OLED_Show_text, "   Wednesday   ");
		else if (MyRTC_Time[6] == 4)
			sprintf(OLED_Show_text, "   Thursday   ");
		else if (MyRTC_Time[6] == 5)
			sprintf(OLED_Show_text, "   Friday   ");
		else if (MyRTC_Time[6] == 6)
			sprintf(OLED_Show_text, "   Saturday   ");
		OLED_ShowString(0, 45, OLED_Show_text, OLED_8X16);

		OLED_Update();
	}

}

void KEY_Proc(void)
{
	if 		(KeyNumber == 1)
	{
		view_menu ++;
		if (view_menu  == 7) view_menu = 0;	//7个界面
	}
	
	else if (KeyNumber == 2)
	{
		if (view_temperature == 0)
		{
			if (view_menu == YEAR)			Set_time_temp[0]++;
			else if (view_menu == MONTH) 	{Set_time_temp[1]++; if (Set_time_temp[1] > 12) {Set_time_temp[1] = 1; Set_time_temp[0]++;}}
			else if (view_menu == DAY )		{Set_time_temp[2]++; if (Set_time_temp[2] > 31) {Set_time_temp[2] = 1; Set_time_temp[1]++;}}
			else if (view_menu == HOUR) 	{Set_time_temp[3]++; if (Set_time_temp[3] > 23) {Set_time_temp[3] = 0; Set_time_temp[2]++;}}
			else if (view_menu == MIN) 		{Set_time_temp[4]++; if (Set_time_temp[4] > 59) {Set_time_temp[4] = 0; Set_time_temp[3]++;}}
			else if (view_menu == SENCEND)	{Set_time_temp[5]++; if (Set_time_temp[5] > 59) {Set_time_temp[5] = 0; Set_time_temp[4]++;}}
		}
	}
	else if (KeyNumber == 3)
	{
		if (view_temperature == 0)
		{
			if (view_menu == YEAR)			{Set_time_temp[0]--; }
			else if (view_menu == MONTH) 	{Set_time_temp[1]--; if (Set_time_temp[1] < 1) {Set_time_temp[1] = 1; }}
			else if (view_menu == DAY )		{Set_time_temp[2]--; if (Set_time_temp[2] < 1) {Set_time_temp[2] = 1; }}
			else if (view_menu == HOUR) 	{if (Set_time_temp[3] > 0) Set_time_temp[3]--;}
			else if (view_menu == MIN) 		{if (Set_time_temp[4] > 0) Set_time_temp[4]--;}
			else if (view_menu == SENCEND)	{if (Set_time_temp[5] > 0) Set_time_temp[5]--; }			
		}
	}                                   
	else if (KeyNumber == 4)
	{
		view_menu = 0;
		MyRTC_Time[0] = Set_time_temp[0];
		MyRTC_Time[1] = Set_time_temp[1];
		MyRTC_Time[2] = Set_time_temp[2];
		MyRTC_Time[3] = Set_time_temp[3];
		MyRTC_Time[4] = Set_time_temp[4];
		MyRTC_Time[5] = Set_time_temp[5];
		
		MyRTC_SetTime();
		Convert_to_Lunar();
	}
	
	else if (KeyNumber == 5)	//anjian 5
	{
		OLED_Clear();
		view_temperature++;

		if (view_temperature  == 2) view_temperature = 0;	//两个界面
	}
}

void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)	//更新中断标志位==SET
	{
		Timer_1ms ++;

		
		if (Timer_1ms>=2000)
		{
			flag_temper_read = 1;
			Timer_1ms = 0;
			PC13_Turn();
		}
		
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);	//清除标志位
		
	}
}
